import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class PropiedadesDialogo extends JDialog implements ActionListener{
	
	/**
	 * Genera la interfaz de opciones
	 */
	private static final long serialVersionUID = 1L;
	final String[] searchstrings = {"Aleatoria Pura", "Aleatoria", "Local", "Multi arranque",
            "Recocido Simulado", "Grasp","Busqueda Tabu","VNS","Genetico"};
	final String[] vecinas = {"Voraz","Precoz","Aleatoria Simple","Aleatoria Precoz"};
	final String[] factort = {"0.01","0.05","0.1","0.8","0.95","0.99"};
	final String[] Grasp_criterio = {"Altura","Ancho","Perimetro","Area"};
	
	JLabel lblbusqueda = new JLabel("Tipo de Busqueda: ");
	JComboBox algoritmos = new JComboBox(searchstrings);
	JComboBox tvecinas = new JComboBox(vecinas);
	JComboBox tfactort = new JComboBox(factort);
	JComboBox grasp_ct = new JComboBox(Grasp_criterio);
	JButton acceptbut = new JButton("Aceptar");
    JButton cancelbut = new JButton("Cancelar");
    JTextField txttam = new JTextField();
    JTextField txtnale = new JTextField();
    JTextField txtnit = new JTextField();
    JTextField txttam_pob = new JTextField();
    JTextField txtejey = new JTextField();
    Ventana dialogowner;
	
	public PropiedadesDialogo(Ventana owner) {
        super();
        this.setSize(300,300);
        this.setLayout(new GridLayout(0, 2));
        dialogowner = owner;
        
        this.add(new JLabel("Tamanio del contenedor:"));
        txttam.setText(""+Ventana.get_tamcontenedor());
        this.add(txttam);
        
        this.add(new JLabel("Eje y"));
        txtejey.setText(""+Ventana.get_ejey());
        this.add(txtejey);
        
        this.add(new JLabel("Poblacion de soluciones"));
        txttam_pob.setText(""+dialogowner.get_tam_pob());
        this.add(txttam_pob);
        
        this.add(new JLabel("N Piezas Aleatorias:"));
        txtnale.setText(""+dialogowner.get_naleatorias());
        this.add(txtnale);
        
        this.add(new JLabel("N iteraciones"));
        txtnit.setText(""+dialogowner.get_niteraciones());
        this.add(txtnit);
       
        this.add(new JLabel("Metodo de resolucion:"));
        this.add(algoritmos);
        algoritmos.setActionCommand("search");
        algoritmos.setSelectedIndex(dialogowner.get_t_algoritmos());
        algoritmos.addActionListener(this);
        
        this.add(new JLabel("Tipo de vecina:"));
        this.add(tvecinas);
        tvecinas.setActionCommand("vecinas");
        tvecinas.setSelectedIndex(dialogowner.get_tvecina());
        tvecinas.addActionListener(this);
        
        this.add(new JLabel("Factor de enfriamiento"));
        this.add(tfactort);
        tfactort.setActionCommand("enfriamiento");
        tfactort.setSelectedIndex(dialogowner.get_enfriamiento());
        tfactort.addActionListener(this);
        
        this.add(new JLabel("Criterio Grasp"));
        this.add(grasp_ct);
        grasp_ct.setActionCommand("graspcriterio");
        grasp_ct.setSelectedIndex(dialogowner.get_criteriogp());
        grasp_ct.addActionListener(this);
        
        this.add(Box.createGlue());
        
        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        acceptbut.setActionCommand("aceptar");
        acceptbut.addActionListener(this);
        aux.add(acceptbut);
        cancelbut.setActionCommand("cancelar");
        cancelbut.addActionListener(this);
        aux.add(cancelbut);
        this.add(aux);
        
        this.pack();
        int x = (int)(owner.getLocation().getX() + (int)((owner.getWidth() - this.getWidth()) / 2));
        int y = (int)(owner.getLocation().getY() + (int)((owner.getHeight() - this.getHeight()) / 2));
        this.setLocation(x, y);
	}
	
	public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "aceptar") {
        	 this.setVisible(false);
        	 int tam = Integer.parseInt(txttam.getText());
        	 if ((tam >= 200) && (tam <= 800))
        		dialogowner.set_tamcontenedor(tam);
        	 
        	 int ale = Integer.parseInt(txtnale.getText());
        	 if ((ale >= 5) && (ale <= 200))
        	 	dialogowner.set_naleatorias(ale);
        	 
        	 int it = Integer.parseInt(txtnit.getText());
        	 if ((it >= 10) && (it <= 100))
        		 dialogowner.set_niteraciones(it);
        	 
        	 int it1 = Integer.parseInt(txtejey.getText());
        	 if ((it1 >= 450) && (it1 <= 1200))
        		 dialogowner.set_ejey(it1);
        	 
        	 int it2 = Integer.parseInt(txttam_pob.getText());
        	 if ((it2 >= 10) && (it2 <= 100))
        		 dialogowner.set_tam_pob(it2);
        }
        else if(e.getActionCommand() == "cancelar") {
        	 this.setVisible(false);
        }
        else 
			if(e.getActionCommand() == "search") {
                dialogowner.set_t_algoritmos(algoritmos.getSelectedIndex());    		
			}
			else
				if (e.getActionCommand() == "vecinas") {
					dialogowner.set_tvecina(tvecinas.getSelectedIndex()); 
				}
				else
					if (e.getActionCommand() == "enfriamiento"){
						dialogowner.set_enfriamiento(tfactort.getSelectedIndex());
					}
					else
						if (e.getActionCommand() == "graspcriterio") {
							dialogowner.set_criteriogp(grasp_ct.getSelectedIndex());
						}
	}

}
